<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>淘有货后台管理系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/static/images/logo/favicon.ico"
          type="image/x-icon"/>
    <%--<link rel="shortcut icon" href="${pageContext.request.contextPath}/font/favicon.ico" type="image/x-icon"/>--%>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/lib/layui-one/css/layui.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/main/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/main/xadmin.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/swiper.min.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/login/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/swiper.jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/lib/layui-one/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/main/xadmin.js"></script>

</head>
<body>
<!-- 顶部开始 -->
<div class="container">
    <jsp:include page="../header.jsp"/>
</div>
<!-- 顶部结束 -->
<!-- 中部开始 -->
<div class="wrapper">
    <!-- 左侧菜单开始 -->
    <jsp:include page="../menu.jsp"/>
    <!-- 左侧菜单结束 -->
    <!-- 右侧主体开始 -->
    <div class="page-content">
        <div class="content">
            <!-- 右侧内容框架，更改从这里开始 -->
            <div class="layui-form-pane" style="text-align: center;">
                <div class="layui-form-item" style="display: inline-block;">
                    <!-- <label class="layui-form-label xbs768">日期范围</label>
        <div class="layui-input-inline xbs768">
          <input class="layui-input" placeholder="开始日" id="LAY_demorange_s">
        </div>
        <div class="layui-input-inline xbs768">
          <input class="layui-input" placeholder="截止日" id="LAY_demorange_e">
        </div>  时间选择 器-->
                    <div class="layui-input-inline">
                        <input type="text" id="roleName" name="username" placeholder="请输入用户名" autocomplete="off"
                               class="layui-input">
                    </div>
                    <div class="layui-input-inline" style="width:80px">
                        <button class="layui-btn" id="btnsearch"><i
                                class="layui-icon">&#xe615;</i></button>
                    </div>
                    <div class="layui-input-inline" style="width:80px">
                        <button class="layui-btn" id="back">返回</button>
                    </div>
                </div>
            </div>

            <xblock>
                <button class="layui-btn layui-btn-danger deletes"><i class="layui-icon">&#xe640;</i>批量删除</button>
                <button class="layui-btn" onclick="user_add()">
                    <i class="layui-icon">&#xe608;</i>添加
                </button>
                <span class="x-right" style="line-height:40px">
                    <c:if test="${count != null} ">
                        共有数据：${count} 条
                    </c:if>
                    <c:if test="${count == null} ">
                        查到数据：1 条
                    </c:if>
                </span>
            </xblock>
            <form action="javascript:;">
                <table class="layui-table">
                    <thead>
                    <tr>
                        <th>
                            <input type="checkbox" id="checkAll" name="" value="">
                        </th>
                        <th>
                            ID
                        </th>
                        <th>
                            用户名
                        </th>
                        <th>
                            电话号码
                        </th>
                        <th>
                            邮箱
                        </th>
                        <th>
                            性别
                        </th>
                        <th>
                            积分
                        </th>
                        <th>
                            操作
                        </th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:if test="${users != null}">
                        <c:forEach items="${users}" var="item" varStatus="tep">
                            <tr>
                                <td>
                                    <input type="checkbox" name="checks" value="${item.uid}">
                                </td>
                                <td>
                                        ${fn:substring(item.uid,0 ,12 )}....
                                </td>
                                <td>
                                        ${item.username}
                                </td>
                                <td>
                                        ${item.phone}
                                </td>
                                <td>
                                      ${item.email}
                                </td>
                                <td>
                                    ${item.sex}
                                </td>
                                <td>
                                        ${item.integral}
                                </td>
                                <td class="td-manage ">
                                    <a title="编辑" href="javascript:;" onclick="user_edit('${item.uid}')"
                                       style="text-decoration:none">
                                        <i class="layui-icon">&#xe642;</i>
                                    </a>
                                    <%--<a title="删除" href="javascript:;"--%>
                                       <%--onclick="user_del(this,'${item.uid}','${item.state}')"--%>
                                       <%--style="text-decoration:none">--%>
                                        <%--<i class="layui-icon">&#xe640;</i>--%>
                                    <%--</a>--%>
                                    <c:if test="${item.state == '0'}">
                                        <span style="width: 56px;" class="layui-btn layui-btn-danger layui-btn-small"
                                              onclick="user_state(this,'${item.username}')">未激活</span>
                                    </c:if>
                                    <c:if test="${item.state == '1'}">
                                        <span style="width: 56px;" class="layui-btn layui-btn-normal layui-btn-small"
                                              onclick="user_state(this,'${item.username}')">激活</span>
                                    </c:if>
                                </td>
                            </tr>
                        </c:forEach>
                    </c:if>
                    <%--<c:if test="${singleUser != null}">--%>
                        <%--<tr>--%>
                            <%--<td>--%>
                                <%--<input type="checkbox" name="checks" value="${singleUser.uid}">--%>
                            <%--</td>--%>
                            <%--<td>--%>
                                <%--1--%>
                            <%--</td>--%>
                            <%--<td>--%>
                                    <%--${singleUser.username}--%>
                            <%--</td>--%>
                            <%--<td>--%>
                                    <%--${singleUser.phone}--%>
                            <%--</td>--%>
                            <%--<td class="td-manage">--%>
                                <%--<a title="编辑" href="javascript:;" onclick="user_edit('${singleUser.uid}')"--%>
                                   <%--style="text-decoration:none">--%>
                                    <%--<i class="layui-icon">&#xe642;</i>--%>
                                <%--</a>--%>
                                <%--<a title="删除" href="javascript:;" onclick="user_del(this,'${singleUser.uid}')"--%>
                                   <%--style="text-decoration:none">--%>
                                    <%--<i class="layui-icon">&#xe640;</i>--%>
                                <%--</a>--%>
                            <%--</td>--%>
                        <%--</tr>--%>

                    <%--</c:if>--%>
                    </tbody>
                </table>
            </form>

            <div id="demo1"></div>
            <!-- 右侧内容框架，更改从这里结束 -->
        </div>
    </div>
    <!-- 右侧主体结束 -->
</div>
<!-- 中部结束 -->
<!-- 底部开始 -->
<!-- 底部结束 -->
<!-- 背景切换开始 -->
<jsp:include page="../imagechange.jsp"/>
<!-- 背景切换结束 -->
<!-- 页面动态效果 -->

</body>
</body>

<script>

    $(function () {
        $("#checkAll").click(function () {
            var checkedOfAll = $("#checkAll").prop("checked");
            $("input:checkbox").prop("checked", checkedOfAll);
        });
        $("input[name='checks']").click(function () {
            var count = 0;
            $("input[name='checks']").each(function () {
                if (!this.checked) {
                    count++;
                }
            });
            if (count == 0) {
                check(true);
            } else {
                $("#checkAll").prop("checked", false);
            }
        });

        function check(flag) {
            $("input:checkbox").prop("checked", flag);
        }

    });
    //批量删除
    $(".deletes").click(function () {
        var values = "";
        $("input[name='checks']").each(function () {
            if (this.checked) {
                values += this.value + ",";
            }

        });
        if (values == "") {
            alert("请您勾选姚删除的用户");
            return;
        }
        var t = window.confirm("您确定要删除所选用户吗");
        if (t) {
            $.ajax({
                url: "${pageContext.request.contextPath}/user/deletes.do",
                type: "post",
                data: {
                    ids: values
                },

                success: function (s) {

                    if (s.success) {
                        alert(s.msg);
                        top.location.href = "${pageContext.request.contextPath}/role.do";
                    } else {
                        alert("存在有未禁用的用户");
                    }
                }
            })
        }

    });
    /*等级-删除*/
    function user_del(obj, id, state) {
        layer.confirm('确认要删除吗？', function (index) {
            if (state == "0") {
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/user/del.do",
                    data: {
                        uid: id
                    },
                    success: function (s) {
                        if (s.success) {
                            //发异步删除数据
                            $(obj).parents("tr").remove();
                            layer.msg('已删除!', {
                                icon: 1,
                                time: 1000
                            });
                        } else {
                            alert(s.msg);
                        }
                    }
                })
            } else {
                layer.msg('用户处于激活状态!', {
                    icon: 1,
                    time: 1000
                });
            }
        });
    }
    // 用户添加
    function user_add() {
        var url = "${pageContext.request.contextPath}/user/toAddRole.do";
        console.log("进入成功");
        x_admin_show(
            "添加用户",
            url,
            "500", "300");
    }
    // 用户修改
    function user_edit(id) {
        var url = "${pageContext.request.contextPath}/user/toEdit.do?uid=" + id;
        x_admin_show(
            "修改用户",
            url,
            "460",
            "310"
        );
    }

    user_state = function (data, username) {
        if ($(data).hasClass("layui-btn-danger")) {
            layer.confirm("操作将激活用户,继续吗？", function () {
                update_state(data, username);
            })
        }
        if ($(data).hasClass("layui-btn-normal")) {
            layer.confirm("操作将禁用用户,继续吗？", function () {
                update_state(data, username);
            })
        }

    }

    update_state = function (data, username) {
        $.ajax({
            url: "${pageContext.request.contextPath}/user/updateState.do",
            data: {
                username: username
            },
            type: "post",
            success: function (d) {
                if (d == "-1") {
                    top.location.href = "${pageContext.request.contextPath}/index.do";
                } else {
                    if (d == '0') {
                        $(data).removeClass("layui-btn-danger");
                        $(data).addClass("layui-btn-normal");
                        $(data).html("激活");
                        layer.msg("成功激活用户", {
                            icon: 1,
                            time: 1000
                        })
                    }
                    if (d == '1') {
                        $(data).addClass("layui-btn-danger");
                        $(data).removeClass("layui-btn-normal");
                        $(data).html("未激活");
                        layer.msg("已禁用用户", {
                            icon: 1,
                            time: 1000
                        })
                    }
                }
            }
        })
    }

    //查找
    $("#btnsearch").click(function () {
        var roleName = $("#roleName").val();
        if(roleName.length == 0 || roleName.indexOf(" ") != -1){
            top.location.reload();
        }
        location.href = "${pageContext.request.contextPath}/user/findUser.do?username=" + roleName;
    })


    $("#back").click(function () {
        location.href = "${pageContext.request.contextPath}/user/role.do";
    })
</script>
</html>
